package com.woniu.utils;

import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.woniu.config.OssProperties;

@Component
public class OssUtil {
	
	@Autowired
	private OssProperties op;
	
	public String uploadFile(String name,String bucketName,InputStream file) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("https://"+op.getEndPoint(), op.getAccessId(), op.getAccessKey());
        String url = null;
        try {
        	 ossClient.putObject(bucketName, name, file);
        	 url = "https://"+bucketName+"."+op.getEndPoint()+"/"+name;
        } finally {
        	if(ossClient != null) ossClient.shutdown();
        }
        return url;
	}
	
	public boolean deleteFile(String name,String bucketName) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("https://"+op.getEndPoint(), op.getAccessId(), op.getAccessKey());
        boolean b = false;
        try {
        	 ossClient.deleteObject(bucketName, name);
        	 b = true;
        } finally {
        	if(ossClient != null) ossClient.shutdown();
        }
        return b;
	}

}
